<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/11/29
 * Time: 14:27
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\api\controller\v1;

use app\api\controller\Base;
use app\common\business\Theatre;
use app\common\business\TheatreCate;
use app\common\business\Venue as BusObj;
use app\common\lib\Show;
use app\common\lib\Status;
use app\common\lib\Time;
use app\common\model\mysql\Collection;
use think\App;
use think\Exception;

class Venue extends Base
{
    protected BusObj $busObj;

    public function __construct(App $app)
    {
        $this->busObj = new BusObj();
        parent::__construct($app);
    }

    /**
     * 获取所有
     * @return \think\response\Json
     */
    public function getVenueList()
    {
        $limit = $this->request->param('limit', 10, 'intval');
        $page = $this->request->param('page', 1, 'intval');
        $address = $this->request->param('address', '', 'trim');

        try {
            $lists = $this->busObj->getVenueListData($limit, $address, '*,thumb as root_path_thumb');
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        return Show::json_show(Status::mini_normal, 'OK', $lists);
    }

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getVenueOrderList()
    {
        $id = $this->request->param('id', 0, 'intval');
        $address = $this->request->param('address', '', 'trim');
        $openid = $this->request->param('openid', '', 'trim');
        if ($id == 0) {
            return Show::json_show(Status::mini_error, "系统错误");
        }
        if (empty($address)) {
            return Show::json_show(Status::mini_error, '地址获取失败');
        }
        //获取剧院
        try {
            $data = $this->busObj->getBaseById($id, '*,thumb as root_path_thumb,thumb_img as root_path_thumb_img');
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }

        if (!$data) {
            return Show::json_show(Status::mini_normal, 'ok', []);
        }
        $order = $data;
        //计算距离
        $order['venue'] = Show::getDistance($order['address_lng'], $order['address_lat'], $address['longitude'], $address['latitude'], 2, 1);
        //查询所有剧场
        try {
            $cate = (new TheatreCate())->getTheatreCateByVenueId($order['id'], '*,title as name');
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        $order['cate'] = $cate;
        //获取key
        $theatre_key = [];
        for ($i = 0; $i < 7; $i++) {
            if ($i == 0) {
                $time = date('Y-m-d');
                $theatre_key[] = [
                    "name" => "今天" . date('m-d', strtotime($time)),
                    "value" => strtotime($time)
                ];
            } else if ($i == 1) {
                $time = date('Y-m-d', strtotime('+1 day'));
                $theatre_key[] = [
                    "name" => "明天" . date('m-d', strtotime($time)),
                    "value" => strtotime($time)
                ];
            } else if ($i == 2) {
                $time = date('Y-m-d', strtotime('+2 day'));
                $theatre_key[] = [
                    "name" => "后天" . date('m-d', strtotime($time)),
                    "value" => strtotime($time)
                ];
            } else {
                $time = date('Y-m-d', strtotime("+{$i} day"));
                $theatre_key[] = [
                    "name" => Show::getTodayWeek(date('w', strtotime("+{$i} day"))) . date('m-d', strtotime($time)),
                    "value" => strtotime($time)
                ];
            }
        }
        $order["theatre_key"] = $theatre_key;
        if (!empty($order['cate'][0])) {
            //查询第一个
            $cate = $order['cate'][0];

            $where = [
                ["venue", "=", $id],
                ["theatre_cate_id", '=', $cate['id']],
                ["start_time", 'between', Time::today()],
                ['start_time', '>', time()]
            ];
            try {
                $theatre = (new Theatre())->getTheatreByVenueId($where, '*,title as name,thumb as root_path_thumb,start_time as get_s_time,end_time as get_e_time');
            } catch (\Exception $e) {
                return Show::json_show(Status::mini_error, $e->getMessage());
            }
            $theatre_list = [];
            if (!empty($theatre)) {
                //查询是否收藏
                foreach ($theatre as $k => $v) {
                    $theatre_list[$k] = $v;
                    $collection = (new Collection())->getCollectionByOpenid($v['id'], $openid);
                    if ($collection) {
                        $theatre_list[$k]['collection'] = 1;
                    } else {
                        $theatre_list[$k]['collection'] = 0;
                    }
                }
            }
            $order['theatre'] = $theatre_list;
        }
        return Show::json_show(Status::mini_normal, 'ok', $order);
    }

    /**
     * @return \think\response\Json
     */
    public function getOrderListByParam()
    {
        //剧院id
        $id = $this->request->param('id', 0, 'intval');
        //剧院id
        $venueId = $this->request->param('venueId', 0, 'intval');
        //剧场id
        $theatreId = $this->request->param('theatreId', 0, 'intval');
        //时间
        $time = $this->request->param('time', 0, 'intval');
//        dump($id);dump($venueId);exit();
        if ($id == 0) {
            return Show::json_show(Status::mini_normal, "没有数据",[]);
        }
        $openid = $this->request->param('openid', '', 'trim');
        try {
            $lists = $this->busObj->getOrderListByParam($id, $theatreId, $time, $openid);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        return Show::json_show(Status::mini_normal, "ok", $lists);
    }
    public function getVuenueGetLocation(){
        $venue_id = $this->request->param('venue_id',0,'intval');
        if($venue_id == 0){
            return Show::json_show(Status::mini_error,"内部错误");
        }
        try {
            $venue = $this->busObj->getBaseById($venue_id);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        if(!$venue){
            return Show::json_show(Status::mini_error,"剧院不存在");
        }
        return Show::json_show(Status::mini_normal,"ok",$venue);
    }
}